﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;

namespace BOSS.Web
{
    /// <summary>
    /// 验证码图片创建器
    /// </summary>
    public class VerifyImageCreator
    {
        /// <summary>
        /// 缺省验证码键
        /// </summary>
        public const string DEFAULT_VERIFY_CODE_KEY = "DEFAULT_VERIFY_CODE_KEY";

        /// <summary>
        /// 验证码文字内容
        /// </summary>
        private static readonly char[] verifyCodeChar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        /// <summary>
        /// 生成验证图片
        /// </summary>
        /// <param name="response"></param>
        /// <param name="codeLength"></param>
        /// <returns></returns>
        public static string CreateImage(HttpResponse response, int codeLength)
        {
            response.Buffer = true;
            response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            response.Expires = 0;
            response.CacheControl = "no-cache";

            string verifyCode = RndNum(codeLength);
            CreateImages(response, verifyCode);
            return verifyCode;
        }

        /// <summary>
        /// 生成验证图片
        /// </summary>
        /// <param name="response"></param>
        /// <param name="checkCode">验证字符</param>
        private static void CreateImages(HttpResponse response, string checkCode)
        {
            int iwidth = (int)(checkCode.Length * 16);
            Bitmap image = new Bitmap(iwidth, 32);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            //定义颜色
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
            //定义字体            
            string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
            Random rand = new Random();
            //随机输出噪点
            for (int i = 0; i < 50; i++)
            {
                int x = rand.Next(image.Width);
                int y = rand.Next(image.Height);
                g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            }

            //输出不同字体和颜色的验证码字符
            for (int i = 0; i < checkCode.Length; i++)
            {
                int cindex = rand.Next(7);
                int findex = rand.Next(5);

                Font f = new Font(font[findex], 12, FontStyle.Bold);
                Brush b = new SolidBrush(c[cindex]);
                int ii = 4;
                if ((i + 1) % 2 == 0)
                {
                    ii = 2;
                }
                g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii);
            }

            //画一个边框
            g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);

            //输出到浏览器
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Jpeg);
            response.ClearContent();
            response.ContentType = "image/Jpeg";
            response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
        }

        /// <summary>
        /// 生成随机的字母
        /// </summary>
        /// <param name="codeLength">生成字母的个数</param>
        /// <returns>string</returns>
        public static string RndNum(int codeLength)
        {
            Random r = new Random((int)DateTime.Now.Ticks);
            char[] result = new char[codeLength];

            for (int i = 0; i < codeLength; i++)
            {
                result[i] = verifyCodeChar[r.Next(verifyCodeChar.Length)];
            }
            return new string(result);
        }
    }
}
